﻿using ExampleService.Infrastructure;
using ExampleService.Xxxxx.SDK.Contract;
using Leo.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace webapiclient.Controllers
{
    /// <summary>
    /// 例子：用户信息
    /// </summary>
    [Route("api/user")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly IExampleContract exampleContract;

        public UserController(IExampleContract exampleContract)
        {
            this.exampleContract = exampleContract;
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Authorize(Roles = "Administrator")] // 需要管理员角色        
        public FuncResult<User> GetUserById(long id)
        {
            return new User
            {
                Id = id,
                Name = "PengW",
                Email = "xxxxx@163.com",
                CreateTime = DateTime.Now,
            };
        }

        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public FuncResult<string> AddUser(User user)
        {
            return "新增数据成功！";
        }

        [HttpPut]
        public FuncResult<string> EditUser(User user)
        {
            return "编辑数据成功!";
        }

        [HttpPatch]
        public FuncResult<string> UpdateUser(User user)
        {
            return "编辑数据成功2!";
        }

        [HttpDelete]
        public FuncResult<string> DeleteUser(long id)
        {
            return "删除数据成功!";
        }


        [HttpGet("token/test")]
        public async Task<FuncResult<string>> GetTokenTest()
        {
            var datas = await exampleContract.GetUserByIdAsync();
            return datas;
        }


    }
}
